﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace WhiteFlow.DevTrackingTool.UI.Converters
{
    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            return ((DateTime)value).ToString("dd/MM/yyyy");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return default(DateTime);

            DateTime date;

            if (DateTime.TryParse(System.Convert.ToString(value), out date))
            {
                return date;
            }
            else
            {
                return default(DateTime);
            }
        }
    }
}